const Router = require('koa-router')
const { Channel } = require('@/app/models/channel')
const { Auth } = require('@/middleware/auth')
const { Op } = require('sequelize')
const { AddChannelValidator, PositiveIntegerValidator, NotEmptyValidator } = require('@/utils/validator')

const router = new Router({
  //统一这是前缀
  prefix: '/api/v1/channel' 
})
//查询栏目列表
router.get('/queryChannel',new Auth().m,async ctx => {
  let { orderName,orderTyep,pageNumber,pageSize,channelName } = ctx.query
  const channel = await Channel.scope('cd').findAndCountAll({
    order: [
      [orderName || 'updated_at', orderTyep || 'DESC']
    ],
    where: {
      name:{
        [Op.like]:'%' + channelName + '%'
      }
    },
    limit: pageNumber || null,
    offset: pageSize ? ((pageSize || 1) - 1) * (pageNumber || 10) : null,
    
  })
  for(let i = 0; i < channel.rows.length; i++){
    if(channel.rows[i].parent_id){
      for(let j = 0;j < channel.rows.length; j++){
        if(channel.rows[i].parent_id == channel.rows[j].id){
          channel.rows[i].name = channel.rows[j].name + '/' + channel.rows[i].name
        }
      }
    }
  }
  
  ctx.body = global.responseData(channel)
})
async function setLineChannel(ctx, type){
  const v = await new NotEmptyValidator().validate(ctx,{
    token: 'ids'
  })
  const idArray = v.get('body.ids').split(',')
  const channel = await Channel.findAll({
    where:{
      id: idArray
    }
  })
  
  if(channel.length > 0){
    channel.forEach(item => {
      item.status = type == 'onlineChannel' ? 1 : 2
      item.save()
    });
    const text = type == 'onlineChannel' ? '上线栏目成功' : '下线栏目成功'
    ctx.body = global.responseData(null, text)
    
  }else{
    ctx.body = global.responseData(null, '未找到栏目', '404')
    
  }
}
//上线栏目
router.post('/onlineChannel',async ctx => {
  await setLineChannel(ctx, 'onlineChannel')
})
//下线栏目
router.post('/outlineChannel',async ctx => {
  await setLineChannel(ctx, 'outlineChannel')
})

//新增栏目
//status： 1：上线  2：下线
router.post('/addChannel', async ctx => {
  const v = await new AddChannelValidator().validate(ctx)
  const channelInfo = {
    name: v.get('body.channelName'),
    parent_id: v.get('body.parentId'),
    identifying_id: v.get('body.identifyingId'),
    icon: v.get('body.icon'),
    images: v.get('body.images'),
    is_nav: v.get('body.isNav'),
    extend_id: v.get('body.extendId'),
    status: 1
  }
  const channel = await Channel.findOne({
    where: {
      name: channelInfo.name,
      parent_id: channelInfo.parent_id
    }
  })
  if(!channel){
    await Channel.create(channelInfo)
    ctx.body = global.responseData(null, '增加栏目成功')
  }else{
    ctx.body = global.responseData(null, '栏目名称重复', '409')
  }
  
})
//删除栏目
router.post('/deleteChannel',async ctx => {
  const v = await new PositiveIntegerValidator().validate(ctx)
  const channel = await Channel.findOne({
    where:{
      id: v.get('body.id')
    }
  })
  if(channel){
    await channel.destroy()
    ctx.body = global.responseData(null, '删除栏目成功')
  }else{
    ctx.body = global.responseData(null, '未找到栏目', '404')
  }
  
})

module.exports = router